package com.example.passwordmanager;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.Ohuang.ilivedata.LiveDataBus;
import com.Ohuang.ilivedata.MyLiveData;
import com.example.passwordmanager.databinding.FragmentSetPasswordBinding;
import com.example.util.base_util.SPUtil;

public class SetPasswordFragment extends Fragment {
     FragmentSetPasswordBinding fragmentSetPasswordBinding;
     private View root;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        root= inflater.inflate(R.layout.fragment_set_password, container, false);
        fragmentSetPasswordBinding=FragmentSetPasswordBinding.bind(root);
        initView();
        return root;
    }

    private void initView(){
        fragmentSetPasswordBinding.imageClose.setOnClickListener(v->{
            getActivity().finish();
        });

        fragmentSetPasswordBinding.imageNext.setOnClickListener(v->{
            fragmentSetPasswordBinding.input.setErrorEnabled(false);
            fragmentSetPasswordBinding.input2.setErrorEnabled(false);
            String s1=fragmentSetPasswordBinding.etPassword.getText().toString();
            String s2=fragmentSetPasswordBinding.etPassword2.getText().toString();
            if (TextUtils.isEmpty(s1)){
                fragmentSetPasswordBinding.input.setError("密码不能为空");
                return;
            }
            if (!TextUtils.equals(s1,s2)){
                fragmentSetPasswordBinding.input2.setError("两次密码输入不一致");
                return;
            }
            savePassword(s1);
            MyLiveData.getInstance().with("startFg",Fragment.class).setValue(new LoginFragment());

        });
    }


    private void savePassword(String s){
        SPUtil.put(getContext(),"password",s);
    }
}